package com.fuelwallet.wizard;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.fuelwallet.R;
import com.fuelwallet.model.CarProfileHandle;
import com.fuelwallet.view.BaseMapActivity;

public class WelcomeActivity extends Activity {
	// Message what
    public static final int MESSAGE_START_NEXT = 0;
    public static final int MESSAGE_NETWORK_OFF = 1;
    
    private CarProfileHandle carProfiles;
    private boolean isFirstTimeLaunch = true;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);        
        
        boolean networkStatus = isNetworkOnline();
        Message msg;
        if(networkStatus == true){
        	msg = handler.obtainMessage(MESSAGE_START_NEXT);        		
        } else {
        	msg = handler.obtainMessage(MESSAGE_NETWORK_OFF);
        }
        handler.sendMessageDelayed(msg, 3900);
        
        carProfiles = new CarProfileHandle(this);
        carProfiles.open();
        if(carProfiles.getAccountNameList().size() == 0)
        	isFirstTimeLaunch = true;
        else
        	isFirstTimeLaunch = false;
        carProfiles.close();
    }
	
	public boolean isNetworkOnline() {
	    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}
	
	private Handler handler = new Handler(){
		 public void handleMessage(Message msg){
			 switch(msg.what){
			 case MESSAGE_START_NEXT:
				 if(isFirstTimeLaunch){
					 Intent intent = new Intent(WelcomeActivity.this, SetupAccountActivity.class); 
					 WelcomeActivity.this.startActivity(intent); 
				 }else{
					 Intent intent = new Intent(WelcomeActivity.this, BaseMapActivity.class); 
					 WelcomeActivity.this.startActivity(intent);
					 overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
				 }
					 WelcomeActivity.this.finish();
				 break;		
			 case MESSAGE_NETWORK_OFF:
				 Toast.makeText(WelcomeActivity.this, "Please enable network connection first! ", Toast.LENGTH_LONG).show();
				 WelcomeActivity.this.finish();
				 break;
			 }
		 }
	};
}